// pages/orderlist/orderlist.js

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myHttp: app.globalData.myHttp,
    myAppointments: '',
  },

  //点击进入我的预约详情页面
  myorder: function(event) {
    console.log(event.currentTarget.dataset.item)
    var currentAppointment = event.currentTarget.dataset.item
    // wx.showToast({
    //   title: '加载中',
    //   icon: 'loading',
    //   duration: 10000
    // })
    wx.navigateTo({
      url: '../appointmentdetails/appointmentdetails?id=' + currentAppointment.id + '&hospitalid=' + currentAppointment.hospitalid
    })

    setTimeout(function() {
      wx.hideToast()
    }, 2000)
  },

  //获取页面数据
  showPage() {
    // wx.showToast({
    //   title: '加载中',
    //   icon: 'loading',
    //   duration: 10000
    // })
    var that = this
    var currentUser = wx.getStorageSync("user")
    var token = "Bearer "+wx.getStorageSync('token')
    wx.request({
      url: this.data.myHttp + '/order/getOrderList',
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': token
      },
      data: {
        // "userId": 60845,
        "userId": currentUser.userID,
        "current": 1,
        "orderType": 2,
        "size": 10,
        "isPay": true
      },
      success: function(res) {
        console.log(res.data)
        console.log(res.data.obj.records)
        var appointment = res.data.obj.records
        var appointmentStatus = []
        var status = ''
        for (let i in appointment) {
          switch (appointment[i].status) {
            case -4:
              status = "等待退款"
              break;
            case -3:
              status = "等待退款"
              break;
            case -2:
              status = "已退款"
              break;
            case -1:
              status = "已取消"
              break;
            case 0:
              status = "订单未生成"
              break;
            case 1:
              status = "待支付"
              break;
            case 2:
              status = "支付成功"
              break;
            case 3:
              status = "退款成功"
              break;
            case 4:
              status = "线下支付取号"
              break;
          }
          appointmentStatus.push(status)
        }
        that.setData({
          myAppointments: res.data.obj.records,
          appointmentStatus: appointmentStatus
        })
        // setTimeout(function () {
        //   wx.hideToast()
        // }, 2000)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.showPage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    setTimeout(function() {
      //complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
    this.showPage()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})